/**
 *  TextMash - simple IDE for Clojure
 *  
 *  Copyright (C) 2010 Aleksander Naszko
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package pl.olek.textmash.workspace;

import java.awt.FontMetrics;

import javax.swing.JTextPane;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.TabSet;
import javax.swing.text.TabStop;

/**
 * 
 * @author anaszko
 *
 */
public class MonoTabSet extends TabSet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4090724406707605771L;

	private static class MyTabStop extends TabStop {

		/**
		 * 
		 */
		private static final long serialVersionUID = -7285515467300267530L;
		float position;

		public MyTabStop() {
			super(0.0f);
		}

		public void setPosition(float position) {
			this.position = position;
		}

		@Override
		public float getPosition() {
			return position;
		}

	};

	MyTabStop stop = new MyTabStop();

	int width;
	int count;
	
	private int getCharWidth(JTextPane pane) {
		FontMetrics fm = pane.getFontMetrics(pane.getFont());
		return fm.charWidth('w');
	}

	private MonoTabSet(int width, int count, JTextPane pane) {
		super(null);
		this.width = width * getCharWidth(pane);
		this.count = count;
	}

	@Override
	public TabStop getTab(int index) {
		stop.setPosition(index * width);
		return stop;
	}

	@Override
	public TabStop getTabAfter(float location) {
		int pos = ((int) location / width);
		int offset = 0;
		if (location - (pos * width) > 0.0f) {
			offset = 1;
		}
		int index = pos + offset;
		stop.setPosition(index * width);
		return stop;
	}

	@Override
	public int getTabCount() {
		return count;
	}

	@Override
	public int getTabIndex(TabStop tab) {
		int pos = ((int) tab.getPosition() / width);
		return pos;
	}

	@Override
	public int getTabIndexAfter(float location) {
		int pos = ((int) location / width);
		int offset = 0;
		if (location - (pos * width) > 0.0f) {
			offset = 1;
		}
		int index = pos + offset;
		return index;
	}

	public static void setTabSize(JTextPane textPane, int charactersPerTab) {
		SimpleAttributeSet attributes = new SimpleAttributeSet();
		StyleConstants.setTabSet(attributes, new MonoTabSet(charactersPerTab,
				64, textPane));
		int length = textPane.getDocument().getLength();
		textPane.getStyledDocument().setParagraphAttributes(0, length,
				attributes, false);
	}
	
	public static void setTabSize(JTextPane textPane, 
			int charactersPerTab, int offset, int length) {
		SimpleAttributeSet attributes = new SimpleAttributeSet();
		StyleConstants.setTabSet(attributes, new MonoTabSet(charactersPerTab,
				64, textPane));
		textPane.getStyledDocument().setParagraphAttributes(offset, length,
				attributes, false);
	}
}
